package com.example.mvc.service;

import com.example.mvc.bean.Book;
import com.example.mvc.bean.BookType;
import com.example.mvc.mapper.BookMapper;
import com.example.mvc.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BookService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    //查询所有
    public List<Book> getBookList(){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //调用方法
        List<Book> bookList =  mapper.getBookList();
        sqlSession.close();
        return bookList;
    }
    //查询书名
    public  List<Book> getQueryBookList(String queryBookName){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //调用方法
        List<Book> bookList =  mapper.getQueryBookList(queryBookName);
        sqlSession.close();
        return bookList;
    }

    public List<BookType> getBookTypeList(){
        //获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //调用方法
        List<BookType> bookTypeList =  mapper.getBookTypeList();
        sqlSession.close();
        return bookTypeList;
    }

    //添加书籍
    public int addBook(Book book){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //4. 调用方法
        int count = mapper.addBook(book);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

        return count;
    }

    public int updateBook(Book book){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //4. 调用方法
        int count = mapper.updateBook(book);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

        return count;
    }

    //通过id删除
    public  int deleteBookById(int bookId){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //4. 调用方法
        int count = mapper.deleteBookById(bookId);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

        return count;
    }

    //添加图书类型
    public int addBookType(BookType bookType){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //4. 调用方法
        int count = mapper.addBookType(bookType);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return count;
    }

    public int updateBookType(BookType bookType){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //4. 调用方法
        int count = mapper.updateBookType(bookType);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return count;
    }

    //删除图书类型
    public int deleteBookTypeById(int bookTypeId) {
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        //4. 调用方法
        int count = mapper.deleteBookTypeById(bookTypeId);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

        return count;
    }
}
